package cn.miju.ser.framework.exception;

import cn.miju.common.content.CodeMessage;
import cn.miju.ser.framework.response.MiJuPayResponse;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * <pre>
 * 自定义异常
 * </pre>
 *
 * @author zhuming
 * @version 1.0.0
 * @since 2023-03-01 15:30
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class ApiException extends RuntimeException {

    private Integer code;
    private String message;
    private CodeMessage codeMessage;

    public ApiException() {
        super();
    }

    public ApiException(CodeMessage codeMessage) {
        super(codeMessage.getMessage());
        this.code = codeMessage.getCode();
        this.message = codeMessage.getMessage();
        this.codeMessage = codeMessage;
    }

    public ApiException(CodeMessage codeMessage, String customMessage) {
        super(codeMessage.getMessage() + customMessage);
        this.code = codeMessage.getCode();
        this.message = codeMessage.getMessage() + customMessage;
        this.codeMessage = codeMessage;
    }

    public ApiException(Integer code, String message) {
        super(message);
        this.code = code;
        this.message = message;
    }

    public <T> ApiException(MiJuPayResponse result) {
        super(result.getMessage());
        this.code = result.getCode();
        this.message = result.getMessage();
    }

    public ApiException(String message) {
        super(message);
        this.code = CodeMessage.FAIL.getCode();
        this.message = message;
    }

    public ApiException(String message, Throwable cause) {
        super(message, cause);
        this.code = CodeMessage.FAIL.getCode();
        this.message = message;
    }

    public ApiException(Throwable cause) {
        super(cause);
        this.code = CodeMessage.FAIL.getCode();
        this.message = cause.getMessage();
    }

    public ApiException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
        this.code = CodeMessage.FAIL.getCode();
        this.message = message;
    }

}
